layui.config({
    base : "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index','form','jquery', 'table'],function(){
    var form = layui.form,
        table=layui.table,
        admin  = layui.admin;
    table.render({
        elem: '#enterprise_Terminal_table'
        ,url: '/agency_mch_terminal/queryData'
        ,method:"post"
        ,height: 500
        ,limit:50							//显示数目
        ,limits:[50,100]
        ,cols: [[
            {field:'id', title: 'ID',width:100, "align":"center"}
            ,{field:'sn',width:200, title: '设备序号',"align":"center"}
            ,{field:'userId',width:100, title: '所属商家ID',"align":"center"}
            ,{field:'type',width:100, title: '设备类型',"align":"center",templet:function (d) {
                        if (d.type=="ALIPAY"){
                            return "支付宝";
                        }else if (d.type=="WECHAT"){
                            return  "微信";
                        }else if (d.type=="QRCODE"){
                            return  "二维码";
                        }else{
                            return "";
                        }

                }}
            ,{field:'createdTime',width:200, title: '创建时间',"align":"center"}
            ,{field:'OPT', width:100, title: '操作',"align":"center",templet:function (d) {
                    return     '<a  href="#" class="layui-btn layui-btn-xs" lay-event="modify_enterpriseTerminal" >编辑</a>';
                }}
        ]]
        ,request:{
            pageName: 'pageNo' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where:{
            userId:$("#mchUserId").val()
        }
        ,id: 'enterprise_Terminal_table'
        ,page: true
    });


    table.on('tool(enterprise_Terminal_table)', function(obj){
        var table_event = obj.event;

        var data = obj.data;

        if(table_event=="modify_enterpriseTerminal"){
            $("#add_enterpriseTerminal_form select[name='type']").attr("disabled",true);

            admin.popup({
                title: '编辑'
                ,shade: 0
                ,anim: -1
                ,offset:"20px"
                ,area: ['80%', '500px']
                ,skin: 'layui-anim layui-anim-upbit'
                ,content:$("#add_enterpriseTerminal_win")
                ,end:function () {
                    table.reload('enterprise_Terminal_table', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                        }
                    });
                }
            });

            form.val('add_enterpriseTerminal_form', {
                "id":data.id,
                "type":data.type,
                "sn":data.sn
            });

        }

    });

    //添加设备
    $(".enterpriseTerminalAdd_btn").click(function(){

        $("#add_enterpriseTerminal_form")[0].reset();
        $("#add_enterpriseTerminal_form select[name='type']").removeAttr("disabled");

        /**
         * 初始化 等级下拉
         */

        admin.popup({
            title: '编辑'
            ,shade: 0
            ,anim: -1
            ,offset:"20px"
            ,area: ['80%', '500px']
            ,skin: 'layui-anim layui-anim-upbit'
            ,content:$("#add_enterpriseTerminal_win")
            ,end:function () {
                table.reload('enterprise_Terminal_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        userId:$("#mchUserId").val()
                    }
                });
            }
        });
        form.val('add_enterpriseTerminal_form', {
            "id":""
        });

    });


    form.on('submit(addenterpriseTerminalOk)',function (data) {
        data.field.userId=$("#mchUserId").val();
        $.ajax({
            url:"/agency_mch_terminal/edit",
            data:data.field,
            type:"post",
            success:function (result) {
                if (result.code == 1){
                    layer.msg("编辑成功",{
                        offset: '32%'
                        ,icon: 1
                        ,time: 0
                        ,btn: '知道了'
                        ,btnAlign:"c"
                        ,shade:0.4
                        ,yes:function () {
                            table.reload('enterprise_Terminal_table', {
                            });
                            layer.closeAll();
                        }
                    })
                }else{
                    layer.msg(result.message,{
                        offset: '32%'
                        ,icon: 2
                        ,time: 3000
                    },function () {
                        layer.closeAll();
                    })
                }

            }
        });
        return false;
    });




});



